package com.aurora.app.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.aurora.app.entity.Help;
import com.aurora.app.entity.HelpComment;
import com.aurora.app.entity.HelpImage;
import com.aurora.app.entity.User;
import com.aurora.app.enums.HelpCategory;
import com.aurora.app.enums.Level;
import com.aurora.app.exption.NullEntityException;
import com.aurora.app.service.HelpCommentService;
import com.aurora.app.service.HelpImageService;
import com.aurora.app.service.HelpService;
import com.aurora.app.service.UserService;
import com.aurora.app.util.DateUtils;
import com.aurora.app.util.StandardResponseBody;
import com.aurora.app.util.WhichLevel;
import com.aurora.base.dto.QueryResult;

@Controller
@RequestMapping(value = "api/help")
public class HelpController {
	@Resource
	private HelpService helpService;
	@Resource
	private HelpImageService helpImageService;
	@Resource
	private HelpCommentService helpCommentService;
	@Resource
	private UserService userService;
	
	@RequestMapping()
	public ModelAndView index(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("help/help");
		return mv;
	}
	
	
	/**
	 * 获取帮帮任务列表
	 * @param request
	 * @param helpCategory
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getHelpByHelpCategory", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	public String getHelpByHelpCategory(HttpServletRequest request,@RequestParam() int helpCategory,@RequestParam() int pageNum,@RequestParam() int pageSize){
		List<Help> hList = null;
		switch(helpCategory){
		case 0:
			hList = this.helpService.getHelpListByCategory((pageNum-1)*pageSize, pageNum, HelpCategory.PackThing);
			break;
		case 1:
			hList = this.helpService.getHelpListByCategory((pageNum-1)*pageSize, pageNum, HelpCategory.BugThing);
			break;
		case 2:
			hList = this.helpService.getHelpListByCategory((pageNum-1)*pageSize, pageNum, HelpCategory.SendThing);
			break;
		}
		
		Map<String,Object> map = new HashMap<String,Object>();
		if(hList == null || hList.size()==0){
			map.put("isLast", true);
			return StandardResponseBody.withPages(1, "", pageNum, true, map);
		}
		List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
		//等级	
		for(Help help:hList){
			Map<String,Object> itemMap = new HashMap<String,Object>();
			itemMap.put("ID",help.getId() );
			itemMap.put("USERIMG",help.getUser().getHeadimgurl() );
			itemMap.put("NICKNAME", help.getUser().getNickName());
			itemMap.put("TIME",DateUtils.getMyFormatTime(help.getCreateTime()) );
			itemMap.put("TITLE", help.getTitle());
			itemMap.put("TASK", help.getTask());
			itemMap.put("LEVEL",(WhichLevel.getLevel(help.getUser().getHuoyueIntegral(), help.getUser().getJiaoyiIntegral()).ordinal()+1)+"");
			
			List<HelpImage> imgList = this.helpImageService.getListByHelpId(help.getId());
			String imgs = "";
			if(imgList!=null&&!imgList.isEmpty()){
				for(HelpImage helpImages:imgList){
					imgs += helpImages.getPath()+",";
				}
				imgs = imgs.substring(0,imgs.length()-1);
			}
			itemMap.put("IMGS",imgs);
			itemMap.put("COMMENTNUM", this.helpCommentService.getCommentNum(help));
			itemMap.put("LOCATION", help.getLocation().getPlace());
			resultList.add(itemMap);
		}
		map.put("RESULTLIST", JSON.toJSONString(resultList));
		return StandardResponseBody.withPages(1, "", pageNum, false, map);
	}

	/**
	 * 获取帮帮任务详细信息
	 * @param request
	 * @param thingId
	 * @return
	 */
	@RequestMapping(value="helpDetail")
	public ModelAndView goodsDetail(HttpServletRequest request,
			@RequestParam("HELP_ID") String helpId){
		ModelAndView mv = new ModelAndView("help/help-detail");
		
		Help help = this.helpService.get(helpId);
		if(help==null){
			throw new NullEntityException("无法获取帮帮任务");
		}
		List<HelpImage> helpImageList = this.helpImageService.getListByHelpId(help.getId());
		String time = DateUtils.getMyFormatTime(help.getCreateTime());
		User user = help.getUser();
		Level level = WhichLevel.getLevel(user.getHuoyueIntegral(), user.getJiaoyiIntegral());
		QueryResult<HelpComment> hResult = this.helpCommentService.getListByHelpByPages(help.getId(), 0, 5);
		long commentNum = hResult.getCount();
		User userLook =(User) request.getSession().getAttribute("user");
		mv.addObject("commentNum",commentNum );
		mv.addObject("helpCommentList",hResult.getResults() );
		mv.addObject("sessionUser", userLook);
		mv.addObject("Help", help);
		mv.addObject("time", time);
		mv.addObject("user", user);
		mv.addObject("level", level.ordinal());
		mv.addObject("helpImageList", helpImageList);
		return mv ;
	}
	
	
	@RequestMapping(produces = "text/plain;charset=UTF-8",value="getComment")
	@ResponseBody
	public String getComment(HttpServletRequest request,
			@RequestParam("HELP_ID") String helpId,
			@RequestParam("PAGEINDEX") Integer pageIndex,
			@RequestParam("PAGESIZE") Integer pageSize
			){
		QueryResult<HelpComment> commentResult = this.helpCommentService.getListByHelpByPages(helpId, pageIndex, pageSize);
		List<HelpComment> commentList = commentResult.getResults();
		Map<String,Object> responseMap = new  HashMap<String,Object>();
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		if(commentList!=null &&commentList.size()>0){
			
			
			for(HelpComment comment:commentList){
				Map<String,Object> resultMap = new  HashMap<String,Object>();
				resultMap.put("HEADIMGURL",comment.getUser().getHeadimgurl());
				resultMap.put("USERID",comment.getUser().getId());
				resultMap.put("NICKNAME", comment.getUser().getNickName());
				resultMap.put("TIME", DateUtils.getMyFormatTime(comment.getCreateTime()));
				resultMap.put("CONTENT", comment.getContent());
				list.add(resultMap);
			}
		}
		responseMap.put("STATUS", "OK");
		responseMap.put("DESC", "返回成功");
		responseMap.put("RESULTLIST", JSON.toJSONString(list));
		responseMap.put("MSGNUM", this.helpCommentService.getCommentNum(this.helpService.get(helpId)));
		return JSON.toJSONString(responseMap);
	}
	
	@RequestMapping(value="addComment")
	@ResponseBody
	public String addComment(HttpServletRequest request,
			@RequestParam("FROM_USER") String fromUserId,
			@RequestParam("TO_USER") String toUserId,
			@RequestParam("HELP_ID") String helpId,
			@RequestParam("CONTENT") String content){
		Map<String,Object> responseMap = new  HashMap<String,Object>();
		
		User from = this.userService.get(fromUserId);
		User to = this.userService.get(toUserId);
		Help help = this.helpService.get(helpId);
		if(from==null || to==null || help == null){
			responseMap.put("STATUS", "ERR");
			responseMap.put("DESC", "找不到实体类");
			return JSON.toJSONString(responseMap);
		}
		HelpComment comment = new HelpComment();
		comment.setUser(from);
		comment.setToUser(to);
		comment.setHelp(help);
		comment.setContent(content);
		if(this.helpCommentService.addComment(comment)){
			responseMap.put("STATUS", "OK");
			responseMap.put("DESC", "评论保存成功");
			return JSON.toJSONString(responseMap);
		}else{
			responseMap.put("STATUS", "ERR");
			responseMap.put("DESC", "评论保存异常");
			return JSON.toJSONString(responseMap);
		}
	}
}
